package com.mb.app.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mb.app.domain.TbProductClass;
import com.mb.app.dao.TbProductClassMapper;
import com.mb.app.service.ITbProductClassService;
import com.mb.common.config.CommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 栏目分类信息  服务实现类
 * </p>
 *
 * @author liliping
 * @since 2019-03-21
 */
@Service
@Slf4j
public class TbProductClassServiceImpl extends ServiceImpl<TbProductClassMapper, TbProductClass> implements ITbProductClassService {

    @Autowired
    private CommonConfig commonConfig;

    @Override
    public List<TbProductClass> queryList(Map<String, Object> map) {
        List<TbProductClass> list = this.baseMapper.lst(map);
        return list;
    }

    @Override
    public int count(Map<String, Object> map) {
        return this.baseMapper.countt(map);
    }

    @Override
    public int save(TbProductClass TbProductClass) {
        TbProductClass.setCreateTime(new Date());
        if(StringUtils.isNotEmpty(TbProductClass.getLogoImg())) {
            String 	logoImg = TbProductClass.getLogoImg();
            logoImg = logoImg.replaceAll(commonConfig.getImgDomain(),"");
            TbProductClass.setLogoImg(logoImg);
        }
        return this.baseMapper.insert(TbProductClass);
    }

    @Override
    public int update(TbProductClass TbProductClass) {
        try {
            TbProductClass TbProductClass1 = new TbProductClass();
            TbProductClass1.setClassId(TbProductClass.getClassId());
            TbProductClass1.setDataStatus(1);
            TbProductClass TbProductClassDO = this.baseMapper.selectOne(TbProductClass1);
            if(TbProductClassDO != null){
                if(StringUtils.isNotEmpty(TbProductClass.getLogoImg())) {
                    String 	logoImg = TbProductClass.getLogoImg();
                    logoImg = logoImg.replaceAll(commonConfig.getImgDomain(),"");
                    TbProductClass.setLogoImg(logoImg);
                }
                TbProductClass.setUpdateTime(new Date());
                this.baseMapper.updateById(TbProductClass);
            }
        } catch (Exception e) {
            log.error("update Exception",e);
        }
        return 0;
    }


    @Override
    public int remove(Long classId,Long delUserId) {
//        return this.baseMapper.deleteById(appId);
        TbProductClass TbProductClass = new TbProductClass();
        TbProductClass.setClassId(classId);
        TbProductClass.setDelTime(new Date());
        TbProductClass.setDataStatus(0);
        TbProductClass.setDelUserId(delUserId);
        return this.baseMapper.updateById(TbProductClass);
    }

    @Override
    public TbProductClass getDtl(Long bannerId) {
        TbProductClass TbProductClass = this.baseMapper.selectById(bannerId);
        if(TbProductClass != null && StringUtils.isNotEmpty(TbProductClass.getLogoImg())) {
            TbProductClass.setLogoImg(commonConfig.getImgDomain()+TbProductClass.getLogoImg());
        }
        return TbProductClass;
    }

    @Override
    public boolean productClassNameExit(Map<String, Object> params) {
        boolean exit;
        exit = this.baseMapper.selectByProductClassInfo(params).size() > 0;
        return exit;
    }

    @Override
    public List<TbProductClass> getProductClassInfoList(Map<String, Object> map) {
        List<TbProductClass> productClasses = this.baseMapper.selectByProductClassInfo(map);
        return productClasses;
    }



}
